/*
 * Systemprogrammierung
 * Multiplayer-Quiz
 *
 * Server
 * 
 * catalog.c: Implementierung der Fragekatalog-Behandlung und Loader-Steuerung
 */

#include <stdbool.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "user.h"
#include "common/rfc.h"

struct KatArray {
	char CatalogName[31];
};

char* shmem;
PACKET activeCatalog;
bool isCatalogChosen = false;

int catCount = 0;

struct KatArray arr[20];

int KatalogAdden(char* name, int i) {
	if (name == NULL || strlen(name) == 0) {
		return -1;
	}
	strncpy(arr[i].CatalogName, name, strlen(name));
	if (i > catCount) {
		catCount = i;
	}
	return 0;
}

int KatalogeSenden(int cSock) {
	PACKET response;
	response.header.type = RFC_CATALOGRESPONSE;
	int i;
	for (i = 0; i <= catCount; i++) {
		response.header.length = htons(strlen(arr[i].CatalogName));
		strncpy(response.content.catalogname, arr[i].CatalogName,
				sizeof(arr[i].CatalogName));
		packetSchreiben(response, cSock);
	}
	response.header.length = htons(0);
	strncpy(response.content.catalogname, "", sizeof(""));
	packetSchreiben(response, cSock);
	return 1;
}

int setActiveCatalog(PACKET packet) {
	activeCatalog = packet;
	isCatalogChosen = true;
	return 0;
}

PACKET getActiveCatalog() {
	return activeCatalog;
}

bool isCatChosen() {
	return isCatalogChosen;
}

void setShMem(char* sh) {
	shmem = sh;
	return;
}

Question* getQuestion(int pos) {
	Question* QPtr = shmem + pos*(sizeof(Question));
	return QPtr;

}
